package com.wwwarehouse.resource_center.fragment.createobject.creategoods;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.GoodsSelectLabelBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.customView.AutoClickButton;
import com.wwwarehouse.resource_center.eventbus_event.GoodsEvent;
import com.wwwarehouse.resource_center.eventbus_event.RulesEvent;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/9/15.
 *         <p/>
 *         新增标签
 */

public class GoodsLabelAddaFragment extends TheParentFragment {

    private AutoClickButton mBtn;
    private EditText mEtText;
    public static final int INSERT_LABER = 1;  //添加标签

    @Override
    protected int getContentId() {
        return R.layout.fragment_goods_label_add_a;
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof GoodsLabelAddaFragment)
            mActivity.setTitle(getString(R.string.good_add_a_leble));
    }

    @Override
    protected void init(View view) {
        mBtn = findView(view, R.id.confirm_btn);
        mEtText = findView(view, R.id.set_rule_name);
        mBtn.bindEditText(mEtText);
        ((BaseActivity) mActivity).hideSoftByEditViewIds(new int[]{R.id.set_rule_name});
    }

    @Override
    protected void loadDatas() {
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map map = new HashMap();
                map.put("labelName", mEtText.getText().toString().trim());
                map.put("businessId", sp.getValue("businessId"));
                httpPost(ResourceConstant.INSERT_LABER, map, INSERT_LABER, true, "");

            }
        });
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what) {
            case INSERT_LABER:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    GoodsSelectLabelBean.ListBean goodsSelectLabelBean = JSON.parseObject(commonClass.getData().toString(), GoodsSelectLabelBean.ListBean.class);
                    goodsSelectLabelBean.setSelect(true);
                    EventBus.getDefault().post(new GoodsEvent(GoodsEvent.GOODS_ADD_LABEL, goodsSelectLabelBean));
                    EventBus.getDefault().post(new RulesEvent(1, goodsSelectLabelBean, "notify"));
                    popFragment();
                } else {
                    if (!TextUtils.isEmpty(commonClass.getMsg())) {
                        toast(commonClass.getMsg());
                    }
                }
                break;
        }
    }

    @Override
    public void onReLoad(int code) {

    }
}
